{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "18a89dcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9f78f196",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url(目标餐厅)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 2.准备API_key\n",
    "API_key = \"HnW-iheAgn_GDIkOvg4xOla6b7JnWpEZ\"\n",
    "API_sercret = \"XrA1r-eFxPR7EaDRDNG64VSIKzDGSR-o\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':\"https://tse1-mm.cn.bing.net/th/id/R-C.43ffd667ed06cc63db9684c128337cfe?rik=cqPVQexuC2iW9A&riu=http%3a%2f%2fwww.ukutu.cn%2fupload%2f201510%2f05%2f201510051147331246.jpg&ehk=vmNFcRTxdq72lNQEsKzZwTg883uSwf6sXkCG0p0BbtI%3d&risl=&pid=ImgRaw&r=0\",\n",
    "\n",
    "}\n",
    "r = requests.post(base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4ac8ab94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7ab6844e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647947201,233d9fca-c07e-4933-be92-30bb5086f1e3',\n",
       " 'time_used': 750,\n",
       " 'faces': [{'face_token': '1dff0bb13171201beabccbe552b80cb7',\n",
       "   'face_rectangle': {'top': 179, 'left': 271, 'width': 159, 'height': 159}}],\n",
       " 'image_id': '39uHgR35XKfPLMY2Byik8w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "53ddd558",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1647947201,233d9fca-c07e-4933-be92-30bb5086f1e3\",\"time_used\":750,\"faces\":[{\"face_token\":\"1dff0bb13171201beabccbe552b80cb7\",\"face_rectangle\":{\"top\":179,\"left\":271,\"width\":159,\"height\":159}}],\"image_id\":\"39uHgR35XKfPLMY2Byik8w==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdfe3e5c",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bc9c8e32",
   "metadata": {},
   "outputs": [],
   "source": [
    "#  2.人脸分析（尝试调用人脸分析analyze接口）\n",
    "\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "\n",
    "API_key = \"HnW-iheAgn_GDIkOvg4xOla6b7JnWpEZ\"\n",
    "API_sercret = \"XrA1r-eFxPR7EaDRDNG64VSIKzDGSR-o\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':'1ee82ee5ed62258e0b645a3bec0cefbf',\n",
    "    'return_attributes':'age',\n",
    "}\n",
    "r = requests.post(base_url,params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7d6cd4b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4d38b178",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"time_used\": 60, \"error_message\": \"INVALID_FACE_TOKEN: 637b2252ad321ba565b60313b25d3b2a\", \"request_id\": \"1647947015,96deae1b-6e3a-4711-943e-2441a63348d4\"}'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8dbdc725",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6ade6d86",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'r_analyze' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-17767aa2310d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson_normalize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr_analyze\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faces'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'r_analyze' is not defined"
     ]
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24b54ada",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9ed8ff68",
   "metadata": {},
   "source": [
    "# 3.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a31f5d4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 人脸对比\n",
    "\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "\n",
    "API_key = \"HnW-iheAgn_GDIkOvg4xOla6b7JnWpEZ\"\n",
    "API_sercret = \"XrA1r-eFxPR7EaDRDNG64VSIKzDGSR-o\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':\"\"\n",
    "}\n",
    "r = requests.post(base_url,params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57fcc35a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "843ba05c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "77b0075b",
   "metadata": {},
   "source": [
    "# 4.人脸检测函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4210546f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：能否将一些功能做函数封装，实现更加便捷的API调用？\n",
    "\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
